package com.neulogistics.contrib.procuredcenter.api.controller;

import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsSetCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsSetCommand;
import com.neulogistics.contrib.procuredcenter.api.application.query.IGoodsSetQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.GoodsListViewModel;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.GoodsSetViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@Api(tags = "商品二级分类管理")
@RequestMapping("/procuredCenter/goodsSet")
public class GoodsSetController {
    @Resource
    IMediator mediator;

    @Resource
    IGoodsSetQueryService goodsSetQueryService;

    @ApiOperation("新增二级分类")
    @PostMapping("/create")
    public BaseResponse<String> create(@RequestBody @Validated @NotNull CreateGoodsSetCommand command){

        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改二级分类")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody @Validated @NotNull UpdateGoodsSetCommand command){

        BaseResponse baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id获取二级分类")
    @GetMapping("/get/{setId}")
    public BaseResponse<GoodsSetViewModel> get(@PathVariable(value = "setId") @NotNull @Positive Integer setId){

        GoodsSetViewModel goodsSetViewModel = goodsSetQueryService.getById(setId);

        return ResultUtils.success(goodsSetViewModel);
    }

    @ApiOperation("获取一级分类下所有二级分类")
    @GetMapping("/listByList")
    public BaseResponse<List<GoodsSetViewModel>> listByList(@NotNull @Positive Integer listId){

        List<GoodsSetViewModel> goodsSetViewModels = goodsSetQueryService.listByList(listId);

        return ResultUtils.success(goodsSetViewModels);
    }

    @ApiOperation("获取所有二级分类")
    @GetMapping("/list")
    public BaseResponse<List<GoodsSetViewModel>> list(){

        List<GoodsSetViewModel> goodsSetViewModels = goodsSetQueryService.list();
        
        return ResultUtils.success(goodsSetViewModels);
    }
}
